/**
 * 
 * Open Action
 * -implements a filechooser window and filter
 * its file.
 * 
 * Here we implement the 'open project' action, which
 * pops a file chooser window up and filter the files 
 * to exhibit. The extension for JFEG projects, where 
 * information will be stored (data files, preferencs,
 * etc) will be jfeg, e.g., 'main project.jfeg'.
 * 
 */

package mainwindow;

import javax.swing.AbstractAction;
import java.awt.event.*;
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class OpenAction extends AbstractAction 
{  
	public OpenAction()
	{    
		super ("Open");
	}

	public void actionPerformed (ActionEvent ev) 
	{
		final JFileChooser fchoose = new JFileChooser();
//	    FileFilter filter1 = 
//	    	new ExtensionFileFilter("JPG and JPEG", new String[] { "JPG", "JPEG" });
		fchoose.setAcceptAllFileFilterUsed(false);
		fchoose.addChoosableFileFilter(new ProjectsFilter());
		
		int returnVal = fchoose.showOpenDialog(fchoose);
		
		if (returnVal == JFileChooser.APPROVE_OPTION) 
		{
            File file = fchoose.getSelectedFile();
			System.out.print("Opening: " + file.getName() + ".\n");
		}
		else 
		{
		}
	}
}


class ProjectsFilter extends FileFilter 
{
	public boolean accept(File f) 
	{	
		if( f.isDirectory() ) 
		{
			return(true);
		}
		
	    String s = f.getName();
	    int i = s.lastIndexOf('.');

	    if(i > 0 && i < s.length() - 1)
	    {
	    	if(s.substring(i + 1).toLowerCase().equals("jfeg"))
	    	{
	    		return(true);
	    	}
	    }

	    return(false);
	}

	public String getDescription() 
	{
		return "Only JFEG projects are visible to browser.";
	}
}